package com.zhxh.nzpos.goods;

import java.util.ArrayList;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhxh.nzpos.R;
import com.zhxh.nzpos.sell.ItemDeleteCallback;

public class GoodsListAdapter extends BaseAdapter {

	private ArrayList<GoodsItem> items;
	private LayoutInflater inflater;
	private ItemDeleteCallback deleteCallback;
	
	public GoodsListAdapter(LayoutInflater inflater, ArrayList<GoodsItem> dealItems, ItemDeleteCallback deleteCallback) {
		this.items = dealItems;
		this.inflater = inflater;
		this.deleteCallback = deleteCallback;
	}
	
	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public Object getItem(int position) {
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		//if (convertView == null) {
			convertView = inflater.inflate(R.layout.goods_list_item, null);
			holder = new ViewHolder();
			holder.identifier = (TextView) convertView.findViewById(R.id.goods_identifier_et);
			holder.name = (TextView) convertView.findViewById(R.id.goods_name_et);
			holder.specification = (TextView) convertView.findViewById(R.id.goods_specification_et);
			holder.category = (TextView) convertView.findViewById(R.id.goods_category_et);
			holder.price = (TextView) convertView.findViewById(R.id.goods_price_et);
			holder.unit = (TextView) convertView.findViewById(R.id.goods_unit_et);
			holder.price2 = (TextView) convertView.findViewById(R.id.goods_price2_et);
			holder.unit2 = (TextView) convertView.findViewById(R.id.goods_unit2_et);
			
		//	holder.code = (TextView) convertView.findViewById(R.id.goods_code_et);
			holder.points = (TextView) convertView.findViewById(R.id.goods_points_et);
			
			holder.deleteBtn = (ImageView) convertView.findViewById(R.id.goods_delete_btn);
			holder.deleteBtn.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (deleteCallback != null) {
						deleteCallback.onDelete(position);
					}
				}
			});
			convertView.setTag(holder);
		//} else {
		//	holder = (ViewHolder) convertView.getTag();
		//}
		
		GoodsItem item = items.get(position);
		if (item != null) {
			holder.identifier.setText(item.getId());
			holder.name.setText(item.getName());
			holder.specification.setText(""+item.getSpecification());
			holder.category.setText(""+item.getCategoryName());
			holder.price.setText(""+item.getPrice1());
			holder.unit.setText(""+item.getUnits1());
			
			holder.price2.setText(""+item.getPrice2());
			holder.unit2.setText(""+item.getUnits2());
		//	holder.code.setText(""+item.getCode());
			holder.points.setText(""+item.getPoints());
		}
		return convertView;
	}

	static class ViewHolder {
		TextView identifier;
		TextView name;
		TextView specification;
		TextView category;
		TextView price;
		TextView unit;
		TextView unit2;
		TextView price2;
		//TextView code;
		TextView points;
		ImageView  deleteBtn;
	}
}
